package FlowGraph.Nodes;

import antlr.collections.AST;

import java.util.*;

import FlowGraph.*;
import FlowGraph.EdgeInfo.*;

//////////////////////////////////////////////////////////////////////////
//// IfEndNode
/**

    O hasThen e hasElse indicam se os respectivos blocos nao acabam em
    quebra de escopo. No caso do hasElse, o hasElse pode ser verdade
    ate quando o bloco nao esta implementado.

@author Ivan Jeukens
@version v 0.1 03/10/2001
*/
public class IfEndNode extends FlowGraphNode {

    public IfEndNode(AST statement) {
        super(statement, IF_END, 3, 1);
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////
    
    public FlowGraphEdge getThenEdge() {
        Iterator ite = _inputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isThen()) {
                return edge;
            }
        }
        return null;
    }
    
    public FlowGraphNode getThenNode() {
        FlowGraphEdge edge = getThenEdge();
        if(edge != null) return edge.getFrom();

        return null;
    }

    public FlowGraphEdge getElseEdge() {
        Iterator ite = _inputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isElse()) {
                return edge;
            }
        }
        return null;
    }

    public FlowGraphNode getElseNode() {
        FlowGraphEdge edge = getElseEdge();
        if(edge != null) return edge.getFrom();

        return null;
    }

    public String toString() {
        return new String("end of if(" +  ")");
    }

    public boolean hasThen() {
        Iterator ite = _inputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isThen()) {
                FlowGraphNode from = edge.getFrom();
                if(from.getType() == FlowGraphNode.IF_BEGIN) {
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean hasElse() {
        Iterator ite = _inputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isElse()) {
                FlowGraphNode from = edge.getFrom();
                if(from.getType() == FlowGraphNode.IF_BEGIN) {
                    return false;
                }
                else {
                    return true;
                }
            }
        }

        return false;
    }

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

}
